﻿#region NeoPatterns
// 
// NeoPatterns framework is released under LGPLv3 license.
// See http://www.opensource.org/licenses/lgpl-3.0.html for more details.
// 
// Please visit http://neopatterns.wikidot.com for more information about NeoPatterns
// 
// Copyright (c) Pascal Craponne 2008
// 
#endregion

using System.Configuration;
using System.Reflection;
using NeoPatterns.Setup.MetaAssembly;

namespace NeoPatterns.Setup
{
    public class WebConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("metaAssembly", IsRequired = false)]
        public string MetaAssemblyName
        {
            get
            {
                return (string)this["metaAssembly"];
            }
        }

        private static WebConfiguration current;
        public static WebConfiguration Current
        {
            get
            {
                return current;
            }
        }

        public static void Load()
        {
            if (current == null)
            {
                var neoPatternsConfiguration = (WebConfiguration)ConfigurationManager.GetSection("NeoPatterns");
                // for some reasons (that we'll need to justify) the section can be omitted
                if (neoPatternsConfiguration == null)
                    return;

                var assemblyName = neoPatternsConfiguration.MetaAssemblyName;
                if (!string.IsNullOrEmpty(assemblyName))
                {
                    var assembly = Assembly.Load(assemblyName);
                }
                // now, all required assemblies are loaded, we can parse them
                MetaAssemblyLoader.Run();
                current = neoPatternsConfiguration;
            }
        }
    }
}